package ferp.android.views.table.controller;

import android.graphics.Point;
import android.view.MotionEvent;
import android.view.View;
import ferp.android.views.CardView;
import ferp.android.views.table.TableView;
import ferp.core.game.Game;
import ferp.core.game.Settings;

/**
* User: olegs
* Date: 05/01/2015 11:58
*/
public class Controller
{
  // selected card offset (both horizontal and vertical)
  public static int OFFSET;

  public Controller(TableView.Listener listener, TableView table)
  {
    this.listener = listener;
    this.table = table;
  }

  public void reset() {resetState();}
  public void resetState() {}

  public void onMovingStarted(int player, CardView cv, Point touch) {}
  public void onMoving(CardView cv, int x, int y) {}
  public void onMovingEnded(CardView cv) {}

  public void onTouch(View view, MotionEvent motionEvent) {listener.onTouch();}
  public void onInputButtonClicked(View view, int id) {listener.onInputButtonClicked(view, id);}
  public void onOfferButtonClicked(View view) {}

  public int elevation(int player, CardView cv) {return 0;}
  public int shift(int player, CardView cv) {return 0;}

  public void select(int player, CardView cv)
  {
    mark(cv, Mark.HIDE);
    cv.layout(cv.getLeft() + shift(player, cv), cv.getTop() + elevation(player, cv));
  }

  public void deselect(int player, CardView cv)
  {
    cv.layout(cv.getLeft() - shift(player, cv), cv.getTop() - elevation(player, cv));
    mark(cv, Mark.SHOW);
  }

  public void layout(int player, CardView cv, int left, int top)
  {
    cv.layout(left + shift(player, cv), top + elevation(player, cv));
    mark(cv, Mark.LAYOUT);
  }

  // mark actions
  protected static enum Mark {SHOW, HIDE, LAYOUT}

  // return current game - do not pass game as parameter to controller as it might change to 'replay'
  protected Game game() {return table.game();}
  protected Settings settings() {return table.settings();}

  // gets index of mark element in TableView 'mark' array
  protected int getMarkIndex(Game g, CardView cv) {return NO_MARK_CARD_INDEX;}

  protected void mark(CardView cv, Mark mark)
  {
    Game g = game();

    if (g.tutorial())
    {
      int index = getMarkIndex(g, cv);

      if (index != NO_MARK_CARD_INDEX)
        switch (mark)
        {
          case SHOW:
            table.showMark(g.player.current, cv, index);
            break;

          case HIDE:
            table.hideMark(index);
            break;

          case LAYOUT:
            table.layoutMark(g.player.current, cv, index);
            break;
        }
    }
  }

  protected TableView.Listener listener;
  protected TableView table;

  private final static int NO_MARK_CARD_INDEX = -1;
}
